/*
 * User: Sepheus Angelus
 * Date: 28/02/2008
 * Time: 14:26
 * 
 */
using System;
using System.Net.Sockets;
using System.Net;

namespace Aphyros_MUD_Server
{
	class Program
	{
		private static int _PortNumber;
		private static int _BacklogSize;
		private static string _WorldName;
		
		#region Properties
		public static int PortNumber
		{
			set { _PortNumber = value; }
			get { return _PortNumber; }
		}
		
		public static int BacklogSize
		{
			set { _BacklogSize = value; }
			get { return _BacklogSize; }
		}
		
		public static string WorldName
		{
			set { _WorldName = value; }
			get { return _WorldName; }
		}
		
		#endregion
		
		public static void Main(string[] args)
		{
			Console.ForegroundColor = ConsoleColor.Green;
			Console.WriteLine("Welcome to the Aphyros MUD Server!");
			Console.WriteLine("Reading settings file...");
			if(SettingsReader.Read())
			{
				Console.ForegroundColor = ConsoleColor.Green;
				Console.WriteLine("Initializing Connection...");
				Database.Connect();
				Database.LoadSkills();
				Database.LoadClasses();
				Database.LoadRooms();
				Console.ForegroundColor = ConsoleColor.Green;
				Console.WriteLine("Loading Scripting Engine...");
				ScriptEngine.Initialize();
				try
				{
					Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
					server.Bind(new IPEndPoint(IPAddress.Any, _PortNumber));
					server.Listen(_BacklogSize);
					Console.ForegroundColor = ConsoleColor.Green;
					Console.WriteLine("Listening for connections on port {0}.",_PortNumber);
					while (true)
					{
						Socket conn = server.Accept();
						new Listener(conn);
					}
				}
				catch
				{
					Console.ForegroundColor = ConsoleColor.Red;
					Console.WriteLine("Could not start server.");
				}

			}
			Console.ReadLine();
		}
		
	}
}
